define(['moment', 'JDA'], function (moment, JDA) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config', '$calculater',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $calculater) {
            $scope.pieChart_config = {
                innerRadiusRate: 0.6,
                isShowLabel: false,
                radius: 80
            };
            $scope.pie_data_users = [
                {id: 'WORKUSERS', label: $filter('translate')('mobile07.work_users'), color: '#68bd84', value: 0, focus: true },
                {id: 'UNWORKUSERS', label: $filter('translate')('mobile07.unwork_users'), color: '#cccccc', value: 0 }

            ];
            $scope.now_status = $scope.pie_data_users[0].id;
            $scope.query_result = [];

            $scope.init = function(){
                init();
            };

            $scope.back = function(){
                $rootScope.changeProgram('menu', {}, true);
            };

            $scope.changeTab = function(item){
                $scope.pie_data_users.forEach(function(data, index){
                    if(data.id == item.id){
                        $scope.now_status = item.id;
                        data.focus = true;
                    } else {
                        data.focus = false;
                    }
                });
            };

            var total_basis_data_qty = 0;
            $scope.search = function(callback){
                $scope.query_result = [];
                getBasisData(function(report_data){
                    total_basis_data_qty = report_data['TOTALUSERS'];
                    Object.keys(report_data).forEach(function(key){
                        caculatePieChart(key, report_data[key]);
                    });
                    getWorkUser(function(report_data){
                        $scope.query_result = $scope.query_result.concat(report_data);
                        getUnWorkUser(function(report_data){
                            $scope.query_result = $scope.query_result.concat(report_data);
                            if (callback)
                                callback();
                        });
                    });
                });
            };

            // 資料細節開窗
            $scope.showItemDetail = function(item){
                $mdDialog.dialog(
                    'module/moduleMobile/program/mobile07/ItemDetail.tmp.html',
                    function(dialog){
                        return {
                            title: $filter('translate')('mobile07.user_name') + item.USERNAME,
                            itemDetail: [],
                            beforeShown: function(){
                                getLaborsMachine(item.USERNO, function(data){
                                    dialog.itemDetail = data;
                                });
                            },
                            back : function(){
                                dialog.hide();
                            },
                            confirm : function(){

                            }
                        };
                    }
                );
            };

            function init(){
                $scope.search();
            }

            function getPercentScale(value){
                if(total_basis_data_qty > 0)
                    return $calculater.decimalMutiple((+value / +total_basis_data_qty), 100).toPrecision(2);
                else
                    return 0;
            }

            // 回寫上下工人員佔多少數量
            function caculatePieChart(id, numerator){
                var filterUser = $filter('filter')($scope.pie_data_users, {id: id}, true);
                if(filterUser.length > 0)
                    filterUser[0].value = numerator;
            }

            //取得人員現況基本資料
            function getBasisData(callback){
                var content = {
                    report_id: 'SR01',
                    query_condition_info: []
                };
                userCustomReportDataGet(content, function(data){
                    $scope.loading_time = moment().format('YYYY-MM-DD HH:mm:ss');
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data[0]);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            // 取得人員上工明細
            function getWorkUser(callback){
                var content = {
                    report_id: 'SR0101',
                    query_condition_info: []
                };
                userCustomReportDataGet(content, function(data){
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    }
                });
            }

            // 取得人員下工明細
            function getUnWorkUser(callback){
                var content = {
                    report_id: 'SR0103',
                    query_condition_info: []
                };
                userCustomReportDataGet(content, function(data){
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    }
                });
            }

            //上工人員所屬設備
            function getLaborsMachine(labor_no, callback){
                var content = {
                    report_id: 'SR0102',
                    query_condition_info: [
                        {column_name: "a.USERNO", query_mode: "0", merge_condition_model: "AND", value: labor_no}
                    ]
                };
                userCustomReportDataGet(content, function(data){
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            function userCustomReportDataGet(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_SYS.user_custom_report_data_get',
                    content : content,
                    success : function(data){
                        if(callback){
                            callback(data);
                        }
                    }
                });
            }
        }
    ];
});
